from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from accounts.models import UserProfile


class Command(BaseCommand):
    help = '创建超级用户'

    def add_arguments(self, parser):
        parser.add_argument(
            '--username',
            type=str,
            default='admin',
            help='超级用户用户名（默认：admin）'
        )
        parser.add_argument(
            '--email',
            type=str,
            default='admin@example.com',
            help='超级用户邮箱（默认：admin@example.com）'
        )
        parser.add_argument(
            '--password',
            type=str,
            default='admin123',
            help='超级用户密码（默认：admin123）'
        )
        parser.add_argument(
            '--first-name',
            type=str,
            default='系统',
            help='名字（默认：系统）'
        )
        parser.add_argument(
            '--last-name',
            type=str,
            default='管理员',
            help='姓氏（默认：管理员）'
        )
        parser.add_argument(
            '--department',
            type=str,
            default='信息技术部',
            help='部门（默认：信息技术部）'
        )
        parser.add_argument(
            '--position',
            type=str,
            default='系统管理员',
            help='职位（默认：系统管理员）'
        )

    def handle(self, *args, **options):
        username = options['username']
        email = options['email']
        password = options['password']
        first_name = options['first_name']
        last_name = options['last_name']
        department = options['department']
        position = options['position']
        
        self.stdout.write(self.style.SUCCESS('开始创建超级用户...'))
        
        # 检查用户是否已存在
        if User.objects.filter(username=username).exists():
            self.stdout.write(
                self.style.WARNING(f'用户 "{username}" 已存在，跳过创建。')
            )
            return
        
        # 创建超级用户
        try:
            user = User.objects.create_superuser(
                username=username,
                email=email,
                password=password,
                first_name=first_name,
                last_name=last_name
            )
            
            # 更新用户资料
            if hasattr(user, 'profile'):
                profile = user.profile
            else:
                profile = UserProfile.objects.create(user=user)
            
            profile.department = department
            profile.position = position
            profile.save()
            
            self.stdout.write(
                self.style.SUCCESS(
                    f'超级用户创建成功！\n'
                    f'用户名: {username}\n'
                    f'邮箱: {email}\n'
                    f'密码: {password}\n'
                    f'姓名: {last_name}{first_name}\n'
                    f'部门: {department}\n'
                    f'职位: {position}'
                )
            )
            
        except Exception as e:
            self.stdout.write(
                self.style.ERROR(f'创建超级用户失败: {str(e)}')
            )